﻿using System;
using Microsoft.Practices.ObjectBuilder;
using CMS.Model;
using CMS.Logging;
using System.Collections.Generic;

namespace CMSWCSFSolution.CMS.Views
{
    public partial class UpdateStudent : BasePage, IUpdateStudentView
    {
        #region Ptotected Method
        private UpdateStudentPresenter _presenter;
        private int _studentid;
        [CreateNew]
        public UpdateStudentPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            _studentid = Convert.ToInt32(Request["ID"]);
            if (!this.IsPostBack)
            {
                try
                {  
                    _presenter.OnSelectedAllGrades();
                    Student student = _presenter.OnSelectStudentById();
                    this.txtStudentName.Text = student.StudentName.Trim();
                    if (student.StudentGender.Trim().ToLower() == "female")
                    {
                        this.RadioBtnFemale.Checked = true;
                    }
                    else
                    {
                        this.RadioBtnMale.Checked = true;
                    }
                    this.ddlClassName.SelectedValue = student.Grade.ClassId.ToString().Trim();
                }
                catch (Exception ee)
                {
                    Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.Highest);
                }
            }
        }
        /// <summary>
        /// Handles the Click event of the BtnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                _presenter.OnUpdateStudent();
                ShowMessage(1, "Save");
            }
            catch (Exception ee)
            {
                Logging.LogWrite(ee.Message.ToString(), Logging.Category.Error, Logging.Priority.High);
            }
        }
        #endregion

        #region Public Property
        /// <summary>
        /// Sets the bind class.
        /// </summary>
        /// <value>
        /// The bind class.
        /// </value>
        public List<Grade> BindClass
        {
            set
            {
                ddlClassName.DataSource = value;
                ddlClassName.DataValueField = "ClassID";
                ddlClassName.DataTextField = "ClassName";
                ddlClassName.DataBind();
                this.ddlClassName.Items.Insert(0, "Please Select");
                this.ddlClassName.SelectedIndex = 0;
            }
        }
        /// <summary>
        /// Gets the student update.
        /// </summary>
        public Student studentUpdate
        {
            get
            {
                Grade grade = new Grade();
                Student student = new Student(grade);
                student.StudentId  = _studentid;
                student.StudentName = txtStudentName.Text.Trim();
                student.StudentGender = (RadioBtnFemale.Checked == true) ? RadioBtnFemale.Text.Trim() : RadioBtnMale.Text.Trim();
                student.Grade.ClassId = int.Parse(ddlClassName.SelectedValue.ToString());
                return student;
            }
        }
        /// <summary>
        /// Gets the student id.
        /// </summary>
        public int StudentId
        {
            get { return _studentid; }
        }
        #endregion
    }
}

